unit clsBackup_PedCpa;

interface

uses Dm, dbtables, biblio, sysutils;

type
  TBackup_PedCpa = class(TObject)
  public
    xEnt_Codigo: string;
    function Existe_Pedido(pedc_numero, ent_codigo: string): boolean;
    procedure Recuperar(bkp_pedido: string; var tq: TQuery);
    function Retorna_Backup(pedc_numero, ent_codigo: string): string;
    function Retorna_Pedido(bkp_pedido, ent_codigo: string): string;
    function Retorna_Pedido2(bkp_pedido: string): string;
    procedure Gravar(bkp_pedido, pedc_numero, ent_codigo: string);
    procedure Excluir(bkp_pedido, ent_codigo: string);
    function Retorna_Pedidos(bkp_pedido, ent_codigo: string; var pedido1, pedido2: string): boolean;
    function RetornaPedidosPorEmpresa(const cStrEmpresas, cStrBackupPedido, cStrEntidade: string; var rStrPedido1, rStrpedido2: string): boolean;
    function Retorna_Ultimo: string;
    function Retorna_Ultimo1: string;
    function MaiorPedido: string;
    function Busca_MaiorMenor(dt_ini, dt_fim: TDateTime; tp: byte; situacao, xemp: string): string;
  end;

implementation

procedure TBackup_PedCpa.Gravar;
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := False;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('Insert into Backup_PedCpa(BKP_PEDIDO, PEDC_NUMERO , ENT_CODIGO) values(:a,:b,:c)');
  tq.Params[0].AsString := strzero(bkp_pedido, 9);
  tq.Params[1].AsString := strzero(pedc_numero, 9);
  tq.Params[2].AsString := strzero(ent_codigo, 5);
  GeraLog('BACKUP_PEDCPA', 'Gravar');
  tq.execsql;
  tq.Free;
end;

procedure TBackup_PedCpa.Excluir;
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.DatabaseName := DmInt.db.DatabaseName;
  tq.Active := False;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('Delete from Backup_PedCpa where bkp_pedido=:a and ent_codigo=:b');
  tq.Params[0].AsString := strzero(bkp_pedido, 9);
  tq.Params[1].AsString := strzero(ent_codigo, 5);
  GeraLog('BACKUP_PEDCPA', 'Excluir');
  tq.execSQL;
  tq.close;
  tq.Free;
end;

function TBackup_PedCpa.Retorna_Pedidos;
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.DatabaseName := DmInt.db.DatabaseName;
  tq.Active := False;
  tq.SQL.Clear;
  tq.Params.Clear;

  if ent_codigo <> '' then begin
    tq.SQL.Add('Select Ent_Codigo, pedc_numero from Backup_PedCpa where bkp_pedido=:a and ent_codigo=:b');
    tq.Params[0].AsString := strzero(bkp_pedido, 9);
    tq.Params[1].AsString := strzero(ent_codigo, 5);
  end else begin
    tq.SQL.Add('Select Ent_Codigo, pedc_numero from Backup_PedCpa where bkp_pedido=:a');
    tq.Params[0].AsString := strzero(bkp_pedido, 9);
  end;

  tq.Open;

  if ent_codigo = '' then begin
    xEnt_Codigo := tq.FieldByName('Ent_Codigo').AsString;
  end;

  if not(tq.eof) then begin
    result := true;
    pedido1 := tq.FieldByName('pedc_numero').AsString;
    tq.next;
    pedido2 := tq.FieldByName('pedc_numero').AsString;
  end else begin
    result := false;
  end;

  if pedido1 = '' then begin
    pedido1 := strzero(bkp_pedido, 9);
  end;

  if pedido2 = '' then begin
    pedido2 := strzero(bkp_pedido, 9);
  end;

  tq.close;
  tq.Free;

end;

procedure TBackup_PedCpa.Recuperar;
begin
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('Select * from Backup_PedCpa where bkp_pedido=:a');
  tq.Params[0].AsString := strzero(bkp_pedido, 9);
  tq.Open;
end;

function TBackup_PedCpa.Existe_Pedido;
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.DatabaseName := DmInt.db.DatabaseName;
  tq.Active := False;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('Select * from Backup_PedCpa where pedc_numero=:a and ent_codigo=:b');
  tq.Params[0].AsString := strzero(pedc_numero, 9);
  tq.Params[1].AsString := strzero(ent_codigo, 5);
  tq.Open;
  if not tq.eof then result := true else result := false;
  tq.close;
  tq.Free;
end;

function TBackup_PedCpa.Retorna_Backup;
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.DatabaseName := DmInt.db.DatabaseName;
  tq.Active := False;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('Select * from Backup_PedCpa where pedc_numero=:a and ent_codigo=:b');
  tq.Params[0].AsString := strzero(pedc_numero, 9);
  tq.Params[1].AsString := strzero(ent_codigo, 5);
  tq.Open;
  result := tq.FieldByName('Bkp_Pedido').AsString;
  tq.close;
  tq.Free;
end;

function TBackup_PedCpa.Retorna_Ultimo;
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.DatabaseName := DmInt.db.DatabaseName;
  tq.Active := False;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('Select max(bkp_pedido) ultimo from Backup_PedCpa');
  tq.Open;
  if tq.FieldByName('ultimo').IsNull then
    result := '1'
  else
    result := tq.FieldByName('ultimo').AsString;
  tq.close;
  tq.Free;
end;

function TBackup_PedCpa.Retorna_Ultimo1: string;
var
  tq: TQuery;
  xp, xb: string;
begin
  tq := TQuery.Create(DmInt);
  tq.DatabaseName := DmInt.db.DatabaseName;
  tq.Active := False;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('Select max(pedc_numero) ultimo from Backup_PedCpa');
  tq.Open;
  if tq.FieldByName('ultimo').IsNull then
    result := '1'
  else begin
    Retorna_Pedidos(tq.FieldByName('ultimo').AsString, '', xp, xb);
    result := xp;
  end;
  tq.close;
  tq.Free;
end;

function TBackup_PedCpa.Retorna_Pedido;
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.DatabaseName := DmInt.db.DatabaseName;
  tq.Active := False;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('Select * from Backup_PedCpa where BKP_PEDIDO=:a and ent_codigo=:b');
  tq.Params[0].AsString := strzero(BKP_PEDIDO, 9);
  tq.Params[1].AsString := strzero(ent_codigo, 5);
  tq.Open;
  if not tq.eof then
    result := tq.FieldByName('PEDC_NUMERO').AsString
  else
    result := '';
  tq.close;
  tq.Free;
end;

function TBackup_PedCpa.Retorna_Pedido2;
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.DatabaseName := DmInt.db.DatabaseName;
  tq.Active := False;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('Select * from Backup_PedCpa where BKP_PEDIDO=:a ');
  tq.Params[0].AsString := strzero(BKP_PEDIDO, 9);
  tq.Open;
  if not tq.eof then
    result := tq.FieldByName('PEDC_NUMERO').AsString
  else
    result := '';
  tq.close;
  tq.Free;
end;

function TBackup_PedCpa.MaiorPedido: string;
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.DatabaseName := DmInt.db.DatabaseName;
  tq.Active := False;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('Select max(pedc_numero) ultimo from Backup_PedCpa');
  tq.Open;
  if tq.FieldByName('ultimo').IsNull then
    result := '000000001'
  else
    result := StrZero(tq.FieldByName('ultimo').asInteger + 1, 9);
  tq.close;
  tq.Free;
end;

function TBackup_PedCpa.Busca_MaiorMenor(dt_ini, dt_fim: TDateTime;
  tp: byte; situacao, xemp: string): string;
var
  p_q: TQuery;
begin
  p_q := TQuery.Create(DmInt);
  p_q.DatabaseName := DmInt.Db.DatabaseName;

  p_q.close;
  p_q.SQL.Clear;
  p_q.Params.Clear;
  p_q.SQL.Add('Select min(bkp_Pedido) as Primeiro, max(bkp_Pedido) as Ultimo from BACKUP_PEDCPA b, PEDIDOCOMPRA p ' +
    '           where (b.pedc_numero||b.ent_codigo = p.pedc_numero||p.ent_codigo) ' +
    '             and Pedc_DataPed >=:dtIni and Pedc_DataPed <=:dtFim ');
  if situacao <> '' then
    p_q.SQL.Add(' and (Pedc_situacao = ' + QuotedStr(situacao) + ') ')
  else
    p_q.SQL.Add(' and (Pedc_situacao = ''P'') ');

  if xemp <> '' then
    p_q.SQL.Add('and Emp_Codigo in (' + xemp + ') ');

  p_q.ParamByName('dtIni').AsDateTime := dt_ini;
  p_q.ParamByName('dtFim').AsDateTime := dt_fim;
  p_q.Open;

  if tp = 0 then
    Result := p_q.FieldByName('primeiro').AsString
  else
    Result := p_q.FieldByName('ultimo').AsString;

  if Result = '' then
    Result := '000000001';

  p_q.Close;
  p_q.Free;
end;

function TBackup_PedCpa.RetornaPedidosPorEmpresa(const cStrEmpresas, cStrBackupPedido, cStrEntidade: string; var rStrPedido1, rStrpedido2: string): boolean;
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.DatabaseName := DmInt.db.DatabaseName;
  tq.Active := False;
  tq.SQL.Clear;
  tq.Params.Clear;

  if cStrEntidade <> '' then begin
    tq.SQL.Add('Select b.Ent_Codigo, b.pedc_numero ' +

               'from Backup_PedCpa b, pedidocompra p ' +

               'where (p.pedc_numero = b.pedc_numero) and ' +
               '      b.bkp_pedido =  ' + QuotedStr(strzero(cStrBackupPedido, 9)) + ' and ' +
               '      b.ent_codigo =' + QuotedStr(strzero(cStrEntidade, 5)) + ' and ' +
               '      p.emp_codigo in(' + cStrEmpresas + ') ');

  end else begin
    tq.SQL.Add('Select b.Ent_Codigo, b.pedc_numero ' +

               'from Backup_PedCpa b, pedidocompra p ' +

               'where (p.pedc_numero = b.pedc_numero) and ' +
               '      b.bkp_pedido = ' + QuotedStr(strzero(cStrBackupPedido, 9)) + ' and ' +
               '      p.emp_codigo in(' + cStrEmpresas + ') ');

  end;

  tq.Open;

  if cStrEntidade = '' then begin
    xEnt_Codigo := tq.FieldByName('Ent_Codigo').AsString;
  end;

  if not(tq.eof) then begin
    result := true;
    rStrPedido1 := tq.FieldByName('pedc_numero').AsString;
    tq.next;
    rStrpedido2 := tq.FieldByName('pedc_numero').AsString;
  end else begin
    result := false;
  end;

  if rStrPedido1 = '' then begin
    rStrPedido1 := strzero(cStrBackupPedido, 9);
  end;

  if rStrpedido2 = '' then begin
    rStrpedido2 := strzero(cStrBackupPedido, 9);
  end;

  tq.close;
  tq.Free;

end;

end.
